package com.ccx.library.module.TaskHandler;

import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaskHandlerService {
    private static final String url = System.getProperty("user.dir")+"\\Library-Module\\Library-core\\src\\main\\java\\com\\ccx\\library\\task\\";

    public static List getTaskHandler(){
        List<String> list = new ArrayList<>();
        File dist = new File(url);
        File[] files = dist.listFiles();
        for (File file : files) {
            list.add(file.getName().substring(0,file.getName().indexOf(".")));
        }
        return list;
    }

    public static Map<String, List> getTaskMethods(String instance) throws ClassNotFoundException {
        Map<String,List> map=new HashMap<>();
        Class<?> aClass = Class.forName("com.ccx.library.task."+instance);
        for (Method declaredMethod : aClass.getDeclaredMethods()) {
            List<String> list=new ArrayList<>();
            if(declaredMethod.getParameters().length > 0) {
                for (Parameter parameter : declaredMethod.getParameters()) {
                    list.add(parameter.getType().getName());
                    map.put(declaredMethod.getName(), list);
                }
            }else{
                list.add("void");
                map.put(declaredMethod.getName(), list);
            }
        }
        return map;
    }
}
